#include "animationtemplate.h"


void CAnimationXMLParser::startElement(void *ctx, const char *name, const char **atts)
{
	_lastelement = (char*)name;
}

void CAnimationXMLParser::endElement(void *ctx, const char *name)
{
	_lastelement = "";
}

void CAnimationXMLParser::textHandler(void *ctx, const char *ch, int len)
{
	string value = std::string((char*)ch,0,len);

	if(_lastelement == "id")
	{
		_lastTemplate = new TEMPLATE_TYPE();
		_lastTemplate->id = atoi(value.c_str());

		CGameTemplate::GetThis()->SetTemplate(_type, _lastTemplate);
	}
	else if(_lastelement == "kind")
		_lastTemplate->kind = atoi(value.c_str());
	else if(_lastelement == "att_front_dist")
		_lastTemplate->att_front_dist = atof(value.c_str());
	else if(_lastelement == "att_back_dist")
		_lastTemplate->att_back_dist = atof(value.c_str());
	else if(_lastelement == "att_plus")
		_lastTemplate->att_plus = atof(value.c_str());
	else if(_lastelement == "impact_time")
		_lastTemplate->impact_time = atoi(value.c_str());
	else if(_lastelement == "mov_dist")
		_lastTemplate->mov_dist = atof(value.c_str());
	else if(_lastelement == "damage_ani_type")
		_lastTemplate->damage_ani_type = atoi(value.c_str());
	else if(_lastelement == "knockback_dist")
		_lastTemplate->knockback_dist = atof(value.c_str());
	else if(_lastelement == "knockdown_time")
		_lastTemplate->knockdown_time = atoi(value.c_str());
	else if(_lastelement == "unusual_condition")
		_lastTemplate->unusual_condition = atoi(value.c_str());
	else if(_lastelement == "unusual_condition_time")
		_lastTemplate->unusual_condition_time = atoi(value.c_str());
	else if(_lastelement == "plist")
		_lastTemplate->plist = value;
	else if(_lastelement == "png")
		_lastTemplate->png = value;
	else if(_lastelement == "png_count")
		_lastTemplate->png_count = atoi(value.c_str());
}

bool CAnimationXMLParser::initParsing()
{
	_animFile = "template/animation.xml";
	_type = GAMETEMPLATE::ANIMATION;


	CCSAXParser parser;

	if (false == parser.init("UTF-8"))
	{
		return false;
	}
	parser.setDelegator(this);

	const char *xmlPath = CCFileUtils::fullPathFromRelativePath(_animFile);
	parser.parse(xmlPath);
	return true;
}
